package com.aiyige.page.publish.selectlocation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.eventbus.EventSelectLocation;
import com.aiyige.page.edituserinfo.EditUserAddressSubChinaPage;
import com.aiyige.page.edituserinfo.model.Province;
import com.aiyige.page.publish.selectlocation.adapter.SelectLocationChinaAdapter;
import com.aiyige.page.publish.selectlocation.model.SelectLocationData;
import com.aiyige.utils.ToastX;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectLocationChinaPage extends AppCompatActivity {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.mainRv)
    RecyclerView mainRv;
    Province province = null;
    SelectLocationChinaAdapter selectLocationChinaAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_address_detail_page);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        try {
            province = JSON.parseObject(intent.getStringExtra(EditUserAddressSubChinaPage.EXTRA_KEY_PROVINCE), Province.class);
            if (province == null) {
                throw new Exception("数据异常");
            }
        } catch (Exception e) {
            ToastX.show(e.getMessage());
            finish();
        }
        titleTv.setText(province.getName());
        selectLocationChinaAdapter = new SelectLocationChinaAdapter();
        mainRv.setLayoutManager(new LinearLayoutManager(this));
        mainRv.setAdapter(selectLocationChinaAdapter);
        selectLocationChinaAdapter.setNewData(province.getCity());
        selectLocationChinaAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Province.City city = selectLocationChinaAdapter.getItem(position);
                SelectLocationData selectLocationData = SelectLocationData.newBuilder()
                        .city(city.getName())
                        .province(province.getName())
                        .cityId(city.getId())
                        .build();
                EventBus.getDefault().post(EventSelectLocation.newBuilder()
                        .resultCode(Activity.RESULT_OK)
                        .data(selectLocationData)
                        .build());
                Intent data = new Intent();
                data.putExtra(SelectLocationPage.EXTRA_KEY_SELECT_LOCATION, selectLocationData);
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }


    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                setResult(RESULT_CANCELED);
                finish();
                break;
        }
    }
}
